package com.atguigu.headline.dao.impl;

import com.atguigu.headline.dao.BaseDao;
import com.atguigu.headline.dao.NewsHeadLineDao;
import com.atguigu.headline.pojo.NewsHeadline;
import com.atguigu.headline.pojo.vo.HeadlineDetailVo;
import com.atguigu.headline.pojo.vo.HeadlinePageVo;
import com.atguigu.headline.pojo.vo.HeadlineQueryVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NewsHeadlineDaoImpl extends BaseDao implements NewsHeadLineDao {

    @Override
    public List<HeadlinePageVo> queryPageList(HeadlineQueryVo headlineQueryVo) {
        List<Object> params=new ArrayList<>();
        String sql="select hid,title,type,page_views pageViews,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher from news_headline where is_deleted=0 ";
        if(headlineQueryVo.getType()!=0 && headlineQueryVo.getType()!=null){
            sql+=" and type= ?";
            params.add(headlineQueryVo.getType());
        }
        if(headlineQueryVo.getKeyWords()!=null && headlineQueryVo.getKeyWords().length()>0){
            sql+="and title like ? ";
            params.add("%"+headlineQueryVo.getKeyWords()+"%");
        }
        sql+="order by pastHours , page_views desc ";
        sql+="limit ? , ?";
        params.add((headlineQueryVo.getPageNum()-1)*headlineQueryVo.getPageSize());
        params.add(headlineQueryVo.getPageSize());
        List<HeadlinePageVo> objects = baseQuery(HeadlinePageVo.class, sql, params.toArray());
        System.out.println("xxxxxx"+objects);
        return objects;
    }

    @Override
    public int queryTotalCount(HeadlineQueryVo headlineQueryVo) {
        List<Object> params=new ArrayList<>();
        String sql="select count(1) from news_headline where is_deleted=0 ";
        if(headlineQueryVo.getType()!=0 && headlineQueryVo.getType()!=null){
            sql+=" and type=?";
            params.add(headlineQueryVo.getType());
        }
        if(headlineQueryVo.getKeyWords()!=null && headlineQueryVo.getKeyWords().length()>0){
            sql+="and title like ? ";
            params.add("%"+headlineQueryVo.getKeyWords()+"%");
        }

        Long totalSize = baseQueryObject(Long.class, sql, params.toArray());
        return totalSize.intValue();
    }



    @Override
    public void increasePageViews(int hid) {
        String sql ="update news_headline set page_views = page_views +1 where hid =?";
        baseUpdate(sql,hid);
    }

    @Override
    public HeadlineDetailVo findHeadlineDetail(int hid) {
        String sql = "select hid,title,article,type, tname typeName ,page_views pageViews,TIMESTAMPDIFF(HOUR,create_time,NOW()) pastHours,publisher,nick_name author from news_headline h left join  news_type t on h.type = t.tid left join news_user u  on h.publisher = u.uid where hid = ?";

        List<HeadlineDetailVo> headlineDetailVo = baseQuery(HeadlineDetailVo.class, sql, hid);
        if(headlineDetailVo!=null && headlineDetailVo.size()>0){
            return headlineDetailVo.get(0);
        }
        return null;
    }

    @Override
    public void insert(NewsHeadline newsHeadline) {
        String sql = "insert into news_headline values(DEFAULT,?,?,?,?,0,NOW(),NOW(),0)";
        baseUpdate(sql,newsHeadline.getTitle(),newsHeadline.getArticle(),newsHeadline.getType(),newsHeadline.getPublisher());
    }

    @Override
    public NewsHeadline query() {
        String sql ="select hid,title,article,type,publisher,page_views pageViews from news_headline where hid =?";
        List<NewsHeadline> newsHeadlines = baseQuery(NewsHeadline.class, sql, 1);
        if(newsHeadlines!=null && newsHeadlines.size()>0){
            return newsHeadlines.get(0);
        }
        return null;
    }

    @Override
    public int update(NewsHeadline newsHeadline) {
        String sql ="update news_headline set title = ?, article= ? , type =? , update_time = NOW() where hid = ? ";
        return baseUpdate(sql,newsHeadline.getTitle(),newsHeadline.getArticle(),newsHeadline.getType(),newsHeadline.getHid());
    }

    @Override
    public int delte(int hid) {
            String sql ="update news_headline set is_deleted =1 ,  update_time =NOW() where hid = ? ";
            return baseUpdate(sql,hid);
    }

}
